﻿
(function () {
    appModule.controller('tenant.views.manuscript.manuscriptChecked', [
        '$scope', '$uibModalInstance', 'abp.services.app.audit', 'manuscriptDatas', 'manuscriptStatus', 'editorUser', 'appSession',
    function ($scope, $uibModalInstance, auditService, manuscriptDatas, manuscriptStatus, editorUser, appSession) {
        var vm = this;

        vm.saving = false;
        vm.manuscriptChecked = null;
        vm.manuscriptDatas = manuscriptDatas;
        vm.audit = {};

        vm.permissions = {
            'finalAudit': abp.auth.hasPermission('Pages.Tenant.Manuscript.FinalAudit')//稿件终审权限
        };

        vm.save = function () {
            vm.saving = true;
            //重新获取数据 select下拉框
            vm.audit.manuscriptStatusID = vm.manuscriptStatusIDselect.value();
            vm.audit.nextUserID = vm.nextUserIDselect.value();
            //退稿时
            var T_manuscriptStatus = null;//退稿状态的id
            var C_manuscriptStatus = null;//草稿状态的id
            for (var i = 0; i < manuscriptStatus.length; i++) {
                if (manuscriptStatus[i].manuscriptStatusName == '退稿') {
                    T_manuscriptStatus = manuscriptStatus[i].id;
                }
                if (manuscriptStatus[i].manuscriptStatusName == '草稿') {
                    C_manuscriptStatus = manuscriptStatus[i].id
                }
            }
            if (vm.audit.manuscriptStatusID == T_manuscriptStatus) {//如果是退稿   
                vm.audit.manuscriptStatusID = C_manuscriptStatus;//稿件状态返回草稿状态
                //vm.audit.nextUserID = '';//清空下一审核人的id
            }
            //退稿时  end
            var postDatas = {};
            postDatas.audit = {};
            postDatas.audit.userId = appSession.user.id;
            postDatas.audit.auditContent = vm.audit.auditContent;
            postDatas.audit.auditStatuId = vm.audit.manuscriptStatusID;
            postDatas.manuscriptId = manuscriptDatas.id;
            postDatas.nextUserID = vm.audit.nextUserID;
            auditService.createAuditStatus(postDatas).success(function () {
                abp.notify.info(app.localize('SavedSuccessfully'));
                $uibModalInstance.close();
            }).finally(function () {
                vm.saving = false;
            });
        };
        vm.cancel = function () {
            $uibModalInstance.dismiss();
        };
        function init() {
            //manuscriptCheckedService.getManuscriptCheckedForEdit({
            //    id: manuscriptCheckedId
            //}).success(function (result) {
            //    vm.manuscriptChecked = result.manuscriptChecked;
            //});

            //获取稿件状态
            vm.manuscriptStatus = manuscriptStatus;
            //稿件状态默认选项
            vm.audit.manuscriptStatusID = manuscriptStatus[0].id;
            var newManuscriptStatus = [];
            for (var i = 0; i < manuscriptStatus.length; i++) {
                if (manuscriptStatus[i].manuscriptStatusName == '审核中') {
                    vm.audit.manuscriptStatusID = manuscriptStatus[i].id;
                }
                if (manuscriptStatus[i].manuscriptStatusName !== manuscriptDatas.manuscriptStatu.manuscriptStatusName && manuscriptStatus[i].manuscriptStatusName !== '草稿' || manuscriptStatus[i].manuscriptStatusName == '审核中') {
                    if (manuscriptStatus[i].manuscriptStatusName == '终审') {
                        if (vm.permissions.finalAudit == true) {
                            newManuscriptStatus.push(manuscriptStatus[i]);
                        }
                    } else {
                        newManuscriptStatus.push(manuscriptStatus[i]);
                    }
                }
            }
            vm.manuscriptStatus = newManuscriptStatus;

            //获取主编用户
            vm.audit.nextUserID = editorUser[0].id;//主编用户默认选项
            vm.editors = {
                dataSource: editorUser,
                dataTextField: "name",
                dataValueField: "id",
                //valueTemplate: '<span>' + editorUser[1].surname + editorUser[1].name + '</span>',
                template: '<span>#: data.surname ##: data.name #</span>'
            };
        }

        init();
    }
    ]);
})();
